package com.cash.channel.svc.service.common;

import com.alibaba.fastjson.JSON;
import com.cash.channel.api.constant.Constant;
import com.cash.channel.api.model.ApiRequestModel;
import com.cash.channel.api.model.ApiResponseModel;
import com.cash.channel.api.model.request.ContractShowRequest;
import com.cash.channel.api.model.response.ContractShowResponse;
import com.cash.channel.api.model.user.ThirdIdModel;
import com.cash.channel.svc.logic.ThirdCommonLogic;
import com.cash.channel.svc.logic.UserLogic;
import com.cash.channel.svc.service.base.ContractShow;
import com.cash.user.model.UserInfoModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

@Slf4j
@Service(Constant.CHANNEL.COMMON+ Constant.SERVICE.合同预览)
public class CommonContractShow implements ContractShow {

    @Autowired
    private UserLogic userLogic;

    @Autowired
    private ThirdCommonLogic thirdCommonLogic;

    @Value("${cash.channel.url.loanProtocolUrl}")
    private String loanProtocolUrl;

    @Value("${cash.channel.url.applyAgreeUrl}")
    private String applyAgreeUrl;

    @Value("${cash.channel.url.serviceUrl}")
    private String serviceUrl;

    @Value("${cash.channel.url.dataQueryAuthUrl}")
    private String dataQueryAuthUrl;

    @Override
    public ApiResponseModel<ContractShowResponse> showContract(ApiRequestModel apiRequestModel) {
        ContractShowRequest contractShowRequest = JSON.parseObject(apiRequestModel.getData(), ContractShowRequest.class);
        try {
            ThirdIdModel thirdIdModel = thirdCommonLogic.findThirdIdModelByThirdOrderId(contractShowRequest.getOrderNo(), apiRequestModel.getChannel());
            UserInfoModel userInfoModel = userLogic.findUserInfoModelByUid(thirdIdModel.getCUserUid());
            String url = null;
            switch (contractShowRequest.getContractNo()) {
//                1委托代扣协议  2居间服务协议，3借款合同，4数据查询授权书
                case 1:
                    url = applyAgreeUrl.concat("?").concat("cName=" + URLEncoder.encode(userInfoModel.getcName(), "UTF-8")).concat("&").concat("cIdno=" + userInfoModel.getcIdno());
                    break;
                case 2:
                    url = serviceUrl.concat("?").concat("cName=" + URLEncoder.encode(userInfoModel.getcName(), "UTF-8")).concat("&").concat("cIdno=" + userInfoModel.getcIdno());
                    break;
                case 3:
                    url = loanProtocolUrl.concat("?").concat("cName=" + URLEncoder.encode(userInfoModel.getcName(), "UTF-8")).concat("&").concat("cIdno=" + userInfoModel.getcIdno());
                    break;
                case 4:
                    url = dataQueryAuthUrl.concat("?").concat("cName=" + URLEncoder.encode(userInfoModel.getcName(), "UTF-8")).concat("&").concat("cIdno=" + userInfoModel.getcIdno());
                    break;
            }
            return new ApiResponseModel<>(true, 0, "", new ContractShowResponse(url));
        }catch (Exception e){
            log.error("通过订单号:{}获取产品信息异常:{}",contractShowRequest.getOrderNo(), e.getMessage(),e);
            return new ApiResponseModel<>(false,0,"获取产品信息异常",null);
        }
    }

}
